/**
 * Filename: GAMutableDictionary.js
 * Author: Zeeshan Amjad
 * Date Created: 2011-05-11
 * Date Updated: 2011-05-13
 */

exports.GAMutableDictionary = Core.Class.subclass({

    classname: 'GAMutableDictionary',

    initialize: function() {
		//this._privateDict = {};
		this._privateDict = new Array();
		this.length = 0;
		this._allKeys = new Array();
    },
	
    initWithDictionary: function(someDictionary)
    {
    	this.removeAllObjects();
    	var allKeys = someDictionary.allKeys();
    	for(var i = 0, count = allKeys.length; i < count; i++)
    	{
    		var key = allKeys[i];
    		var obj = someDictionary.objectForKey(key);
    		if( (obj != undefined) && (obj != null) )
    		{
    			this.setObject(key, obj);
    		}
    	}
    },
    
	setObject:function(key, value) {
		key = key + '';
		this._checkIfReleased();
		if(isValidString(key)) {
			if( (this._privateDict[key] == undefined) || (this._privateDict[key] == null))
			{
			    this.length++;
				this._allKeys[this._allKeys.length] = key;
			}
			this._privateDict[key] = value;
		}
	},
	allKeys : function()
	{
		return this._allKeys;
	},
	
	allValues: function()
	{
		var allKeys = this.allKeys();
		var arr = new Array();
		for ( var i = 0; i < allKeys.length ; i++)
		{
			var key = allKeys[i];
			arr.push(this.objectForKey(key));
		}
		return arr;
		//return this._privateDict;
	},
	
	objectForKey:function(key) {
		key = key + '';
		this._checkIfReleased();
		if(key != null) {
			if(this._privateDict[key] != null) {
				return this._privateDict[key];
			}
		}
		return null;
	},
	
	removeObjectForKey:function(key) {
		key = key + '';
		this._checkIfReleased();
		if(isValidString(key)) {
			if(this.hasKey(key) && this._privateDict[key] != null) {
				//this._privateDict[key] = null;
				this._removeKey(key);
				delete this._privateDict[key];
			} else {
				GALogger.log("Object not removed from the dictionary");
			}
		} else {
			GALogger.log("Invalid key specified while removing an object");
		}
	},
	
	removeAllObjects: function() {
		//harris
		if(this._allKeys)
		{
			delete this._allKeys;
			this._allKeys = null;
		}
		
		if(this._privateDict)
		{
			delete this._privateDict;
			this._privateDict = null;
		}
		
		this.initialize();
		
	},
	
	hasKey: function(key) {
		key = key + '';
		//harris
		if(isValidString(key))
		{
			var foundIndex = this._indexOfKey(key);
			return (foundIndex != -1);	//will return true or false according to the valid key
		}
		
		GALogger.log("Invalid key for dictionary");
		
		return false;	//returns false if an invalid key is sent key should be string
	},
	
	hasObject: function(obj) {
		//harris
		
		/* will be implemented later 
		for(key in this._allKeys)
		{
			if(this._privateDict[key] == obj)
				return true;
		}	
		*/
		return false;
	},
	
	toString: function()
	{
		var str = '(';
		var key = '';
		var allKeys = this.allKeys();
		if(allKeys.length <= 0)
		{
			return '<EMPTY DICTIONARY>';
		}
		for ( var i = 0; i < (allKeys.length - 1); i++)
		{
			key = allKeys[i];
			str += key + ': '+this.objectForKey(key) + ', ';
		}
		key = allKeys[i];
		str += key + ': '+this.objectForKey(key) + ')';
		return str;
	},
	
	print: function()
	{
		var str = this.toString();
		TAUtil.log('GAMutableDictionary: '+str);
	},
	
	destroy:function() {
		if(this._allKeys)
		{
			delete this._allKeys;
			this._allKeys = null;
		}
		
		if(this._privateDict)
		{
			delete this._privateDict;
			this._privateDict = null;
		}
	},
	
	/* private methods for internal implementation */
	
	_indexOfKey: function(key) {
		//harris
		key = key + '';
		return this._allKeys.indexOf(key);
	},
	
	_removeKey: function(key) {
		key = key + '';
		//harris
		var index = this._indexOfKey(key);
		this._allKeys.splice(index,1);
		this.length--;
	},
	
	_checkIfReleased: function()
	{
		if(this._privateDict == null)
		{
			GALogger.log("GAMutableDictionary was released, reinitializing");
			this._privateDict = {};
		}
	}
});